#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>



int main(int argc, const char **argv) {
	/* 
	 * argv[1]: name (like 'font')
	 * argv[2]: binary file
	 * */

	const char *name = argv[1];
	const char *filename = argv[2];

	if (!name || !filename) {
		printf("fatal: no enough arguments. \n");
		printf("usage: bin2s name filename\n");

		return 1;
	}

	int fd = open(filename, O_RDONLY);

	printf("section data\nglobal %s_start\n%s_start:", name, name);

	/* read one byte from file, and put it in hex mode. */

	char c;

	for (;;) {
		int i;

		printf("\n\tdb ");

		for (i = 0; i < 5; i++) {
			if (!read(fd, &c, 1)) 
				goto done;

			if (i) 
				printf(", ");
			
			printf("0x%02x", c & 0xff);
		}
	}

done:

	printf("\nglobal %s_end\n%s_end:\n", name, name);
	printf("global %s_size\n%s_size: dq $ - %s_start\n", name, name, name);

	close(fd);

	return 0;
}
